/*Parser.java Parsing of different events by matching of strings and generates output messages.
Copyright (C) 2013  Dmytro Kostiantynovskyi, HartLi

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Parser {
	private Pattern[] p;
	private ArrayList<String> masks;
	private ArrayList<String> reg;
	private ArrayList<ArrayList<String>> lang;
	private String grid;
	/**
	 * Constructor
	 */
	public Parser(ArrayList<ArrayList<String>> lang){
		this.lang = lang;
		masks = FileReader.getConvertedToArrayWithIndexKey(Config.currentDirectory+Config.sep+Config.relativeInstallPath+Config.quickMasksFile);
		reg = FileReader.getConvertedToArrayWithIndexKey(Config.currentDirectory+Config.sep+Config.relativeInstallPath+Config.regexFile);
		p = new Pattern[reg.size()];
		for(int i = 0; i < reg.size(); i++){
			if(reg.get(i) != null && reg.get(i) != "")
				p[i] = Pattern.compile(reg.get(i));
		}
	}
	
	public ArrayList<ArrayList<String>> getParsedMessage(String s) throws UnknownHostException, IOException{
		ArrayList<ArrayList<String>> arr = null;
		Matcher m;
		for(int i = 0; i < reg.size(); i++){
			if(masks.get(i) != null && s.indexOf(masks.get(i)) != -1){
				m = p[i].matcher(s);
				m.find();
				arr = getOutput(i, m);
				break;
			}
		}
		return arr;
	}
	
	public ArrayList<ArrayList<String>> getOutput(int index, Matcher m) throws UnknownHostException, IOException{
		ArrayList<ArrayList<String>> lst = new ArrayList<ArrayList<String>>();
		lst.add(new ArrayList<String>());
		String ip = "";
		String format = lang.get(index).get(0);
		String time = m.group(1);
		String name = m.group(2);
		String craft = m.group(3);
		String seat = m.group(4);
		String otherpilot = m.group(5);
		String x = m.group(6);
		String y = m.group(7);
		String weapons = m.group(8);
		String fuel = m.group(9);
		if(!x.isEmpty()&& !y.isEmpty())
			grid = Coordinates.getLetter(Double.parseDouble(x))+Coordinates.getNumber(Double.parseDouble(y));
		switch(index){
			case 0:
				Client cl = new Client();
				ip = cl.getClientIP();
				String country = Client.getLocation(ip);
				if(country != "" && country != "N/A"){
					lst.get(0).add(0, String.format(format+"(%s)", name, country));
					break;
				}
/*				else
					lst.get(0).add(0,String.format(format, name));
				lst.get(0).add(1,lang.get(index).get(1));
				lst.get(0).add(2, name);
				break;*/
			case 1:	case 4: case 5:
				lst.get(0).add(0, String.format(format, name));
				break;
			case 2:
				lst.get(0).add(0, String.format(format, name, seat));
				break;
			case 3:
				lst.get(0).add(0, String.format(format, name, grid));
				break;
		}
		lst.get(0).add(1,lang.get(index).get(1));
		lst.get(0).add(2, name);
		return lst;
	}
}
